<?php

//模拟PHP异步操作
public function asyncRequest(){
    //fsockopen初始化一个套接字连接到指定主机，默认情况下将以阻塞模式开启套接字连接
    //$fp = fsockopen($_SERVER['SERVER_ADDR'], $_SERVER['SERVER_PORT'], $errno, $errstr,5);
    $fp = fsockopen('localhost', '80', $errno, $errstr, 5);
    if(!$fp){
        echo "$errstr ($errno)\n";

    }

    //将阻塞模式转换为非阻塞模式
    stream_set_blocking($fp, true);
    stream_set_timeout($fp,1);
    //fputs($fp, "GET /request.php?id=1026900\r\n");
    fputs($fp, "POST /request.php?id=1026900\r\n");       //请求的资源 URL 一定要写对
    usleep(1000);
    //close后，则后续while无法接收到异步请求结果
    fclose($fp);

    //不等待返回结果，直接return true
    echo json_encode(['status'=>200,'message'=>'success']);

    //如果不close源，这里仍可收到异步响应结果
    while (!feof($fp)) {
        $line = fread($fp,4096);
        echo $line;
    }
}

//代码参考：
//https://www.jianshu.com/p/c2af53b30a37
//https://www.cnblogs.com/fps2tao/p/7900536.html